Tutorial 2: ready-to-use benchmarks and visualization

In the previous tutorial, you have learnt how to define your own custom benchmark function using the BenchmarkFunction class. In this tutorial, you will learn how to use the ready-to-use benchmark functions provided by the beeoptimal.benchmarks module. These pre-defined functions are designed to help you quickly test optimization algorithms without having to implement the functions yourself. Additionally, you will get introduced to some visualization tools that allow a better understanding of the optimization landscapes.

At the moment, the available ready-to-use benchmark functions are the following:

  • Sphere

  • Rosenbrock

  • Ackley

  • Rastrigin

  • Weierstrass

  • Griewank

  • Schwefel

  • Sumsquares

  • Eggholder

Except from the Eggholder, which is inherently a 2D function, all the other functions are available in 2,10 and 30 dimensions.

For this tutorial, we will focus on the 2D Griewank function, as its landscape offers interesting features that become more apparent depending on the zoom level we apply.

[2]:
import numpy as np
from beeoptimal.benchmarks import Griewank2d
from beeoptimal.plotting import contourplot,surfaceplot

We can easily evaluate the function in the same way as we did in the previous tutorial.

[3]:
print(f"\nBenchmark:\n {Griewank2d.name}")
print(f"\nDefault Bounds:\n {Griewank2d.bounds}")
print(f"\nOptimal Solution:\n {Griewank2d.optimal_solution}")
print(f"\nOptimal Value:\n {Griewank2d.optimal_value}")

Benchmark:
 Griewank-2d

Default Bounds:
 [[-600  600]
 [-600  600]]

Optimal Solution:
 [0. 0.]

Optimal Value:
 0.0
[4]:
x   = np.random.uniform(low=Griewank2d.bounds[:,0], high=Griewank2d.bounds[:,1], size=len(Griewank2d.bounds))
f_x = Griewank2d.evaluate(x)
print(f"{Griewank2d.name} function evaluated at x={x} --> f(x)={f_x}")
Griewank-2d function evaluated at x=[-572.05440973 -548.83516035] --> f(x)=158.02230710936988

As mentioned above, BeeOptimal provides two visualization tools that facilitate the exploration of the benchmark functions:

  • contourplot

  • surfaceplot

Warning

These visualization tools are designed specifically for 2D functions, as we cannot visualize more than three dimensions (unless you have exceeded the glasses of brandyβ€¦πŸ˜΅β€πŸ’«πŸ₯ƒ)

The most simple way to use them is the following:

[5]:
griewank2d_contourplot = contourplot(
    function=Griewank2d,
    title=f'Contourplot for {Griewank2d.name} function',
    figsize=(600,600)
    )

griewank2d_surfaceplot = surfaceplot(
    function=Griewank2d,
    title=f'Surfaceplot for {Griewank2d.name} function',
    figsize=(600,600)
    )

griewank2d_contourplot.show()
griewank2d_surfaceplot.show()

By default, the plotting functions use the bounds defined in the benchmark function. However, for visualization purposes, you might want to adjust these bounds. The functions allow you to either pass custom bounds as arguments or specify a zoom factor. These two options can also be combined for greater flexibility in your visualizations.

[6]:
p1 = surfaceplot(
    function = Griewank2d,
    title    = f'{Griewank2d.name} Surface [DEFAULT BOUNDS]',
    figsize  = (500,500)
    )

p2 = surfaceplot(
    function = Griewank2d,
    title    = f'{Griewank2d.name} Surface [CUSTOM BOUNDS]',
    figsize  = (500,500),
    bounds   = np.array([[-60,60],[-60,60]]),
    )

p3 = surfaceplot(
    function = Griewank2d,
    title    = f'{Griewank2d.name} Surface [ZOOM]',
    figsize  = (500,500),
    zoom     = 0.02
    )

p4 = surfaceplot(
    function = Griewank2d,
    title    = f'{Griewank2d.name} Surface [CUSTOM BOUNDS & ZOOM]',
    figsize  = (500,500),
    bounds   = np.array([[-0.5,0.5],[-0.5,0.5]]),
    zoom     = 10.0
    )

p1.show()
p2.show()
p3.show()
p4.show()

Note

Both bounds and zoom arguments can be passed to the function contourplot in the same way as to the function surfaceplot.